package other.xsy.passlys.receivable;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEvent;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventRequest;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventResponse;
import com.rkhd.platform.sdk.data.model.*;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import org.apache.commons.lang.StringUtils;
import other.xsy.passlys.util.HCXObjectService;
import other.xsy.passlys.util.ResultVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description deBooking审批通过: 修改booking、流水相关的金额字段
 * @Author 黎炎松
 * 2024-08-14 15:40
 */
public class DeBookingCompleteAfter implements ApprovalEvent {

    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "deBooking审批通过: 修改booking、流水相关的金额字段  ==> ";


    public static void main(String[] args) {
        DeBookingCompleteAfter deBookingCompleteAfter = new DeBookingCompleteAfter();
        ApprovalEventRequest approvalEventRequest = new ApprovalEventRequest();
        approvalEventRequest.setDataId(3421865768884875L);
        deBookingCompleteAfter.execute(approvalEventRequest);
    }

    @Override
    public ApprovalEventResponse execute(ApprovalEventRequest request) {
        logger.info(LOGGER_MSG + "触发开始");
        logger.info("ObjectApiKey:" + request.getEntityApiKey() + " , DataId:" + request.getDataId());

        ReceivableService receivableService = new ReceivableService();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sceneType", 1);
        jsonObject.put("deBookingId", request.getDataId());
        receivableService.calcDeBookingAmount(jsonObject);


        // 获取订单id,计算订单上的booking金额
        try {
            // 延迟2s (xoql有延迟问题)
            Thread.sleep(2000);

            Debooking__c debooking = new Debooking__c();
            debooking.setId(request.getDataId());
            debooking = hcxObjectService.get(debooking);
            CalcOrderBookingAmount calcOrderBookingAmount = new CalcOrderBookingAmount();
            calcOrderBookingAmount.dealOrderBookingAmount(debooking.getOrder__c());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        ApprovalEventResponse response = new ApprovalEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        return response;
    }




}
